class MachinesController < ApplicationController
  layout "standard"
  
  def index
    list
    render :action => 'list'
  end

  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :list }
         

  def list
    @machines = Array.new
    
    all_machines = Machine.find(:all)
    unless params[:list_cat].nil?
      for machine in all_machines
        unless params[:list_cat] == 'Unknown'
          if machine.categories.collect{ |cat| cat.name}.include?( params[:list_cat] )
            @machines.push(machine)
          end
        else
          if machine.categories.collect{ |cat| cat.name }.size == 0
            @machines.push(machine)
          end
        end
      end
      @cat_name = params[:list_cat]
    else
      @machines = Machine.find(:all)
      @cat_name = nil
    end
    
    @all_categories = Category.find(:all, :order=>"name")
    
    @num_of_machines = all_machines.length
  end

  def show
    @machine = Machine.find(params[:id])
    @selected = @machine.categories.collect { |cat| cat.name }
  end
  
  def search
    @search_results = Array.new
    
    unless params[:query].empty?
      @search_query = params[:query]
      q = "%" + params[:query] + "%"
      @search_results = Machine.find(:all, :include => [ :installed_applications ], :conditions => [ "host_name LIKE ? OR operating_system LIKE ? OR ip_address LIKE ? OR mac_address LIKE ? OR user LIKE ? OR hardware LIKE ? OR notes LIKE ? OR name LIKE ?", q, q, q, q, q, q, q, q])
    else
      search_temp = empty
      @search_query = empty
    end
    
    @all_categories = Category.find(:all, :order=>"name")
    
    @category_size_hash = Hash.new
    for category in @all_categories
      @category_size_hash[category.name] = category.machines.collect{|m|}.size
    end
    
    all_machines = Machine.find(:all)
    @num_of_machines = all_machines.length
  end

  def new
    @machine = Machine.new
    @all_categories = Category.find(:all, :order=>"name")
    @selected = []
  end

  def create
    @machine = Machine.new(params[:machine])
    @machine.categories = Category.find(params[:categories]) if params[:categories]
    if @machine.save
      flash[:notice] = 'Machine was successfully created.'
      redirect_to :action => 'list'
    else
      @all_categories = Category.find(:all, :order=>"name")
      render :action => 'new'
    end
  end

  def edit
    @machine = Machine.find(params[:id])
    @all_categories = Category.find(:all, :order=>"name")
    @selected = @machine.categories.collect { |cat| cat.id.to_i }
  end

  def update
    @machine = Machine.find(params[:id])
    @machine.categories = Category.find(params[:categories]) if params[:categories]
    if @machine.update_attributes(params[:machine])
      flash[:notice] = 'Machine was successfully updated.'
      redirect_to :action => 'show', :id => @machine
    else
      render :action => 'edit'
    end
  end

  def destroy
    Machine.find(params[:id]).destroy
    redirect_to :action => 'list'
  end
end
